package es.udc.pfc.davidparedes.model.shift;

import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.persistence.Temporal;

import es.udc.pfc.davidparedes.model.universe.Universe;
import es.udc.pfc.davidparedes.model.universemanager.UniverseManager;

@Entity
public class Shift {
	
	private long shiftId;
	private Calendar start;
	private Calendar end;
	private long duration;
	private char permanent;
	private UniverseManager manager;
	private Universe universe;
	private long version;
	
	public Shift() {
	}
	
	public Shift(Calendar start, Calendar end, char permanent, UniverseManager manager) {
		this.start = start;
		this.end = end;
		this.permanent = permanent;
		this.manager = manager;
		this.universe = manager.getUniverse();
		
		if (start.before(end)) {
			this.duration = end.getTimeInMillis()-start.getTimeInMillis();
		}
	}
	
	@Column (name = "id")
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getShiftId() {
		return shiftId;
	}
	
	public void setShiftId(long shiftId) {
		this.shiftId = shiftId;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getStart() {
		return start;
	}
	
	public void setStart(Calendar start) {
		this.start = start;
	}
	
	@Temporal(TemporalType.TIMESTAMP)
	public Calendar getEnd() {
		return end;
	}
	
	public void setEnd(Calendar end) {
		this.end = end;
	}
	
	public long getDuration() {
		return duration;
	}
	
	public void setDuration(long duration) {
		this.duration = duration;
	}
	
	public char getPermanent() {
		return permanent;
	}
	
	public void setPermanent(char permanent) {
		this.permanent = permanent;
	}
	
	@ManyToOne(optional=false, fetch=FetchType.LAZY)
	@JoinColumn(name="manager")
	public UniverseManager getManager() {
		return manager;
	}
	
	public void setManager(UniverseManager manager) {
		this.manager = manager;
	}
	
	@ManyToOne(optional=false, fetch=FetchType.LAZY)
	@JoinColumn(name="universe")
	public Universe getUniverse() {
		return universe;
	}
	
	public void setUniverse(Universe universe) {
		this.universe = universe;
	}
	
	@Version
	public long getVersion() {
		return version;
	}
	
	public void setVersion(long version) {
		this.version = version;
	}
	
	@Override
	public String toString() {
		return ("Turno [shiftId=" + shiftId + ", start=" + start
				+ ", end=" + end + ", duration=" + duration +", permanent=" + permanent
				+ ", manager=" + manager + ", version="
				+ version + "]");
	}

}
